package com.tencreat.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.api.MsgApi;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.config.OssClient;
import com.tencreat.common.core.config.OssFactory;
import com.tencreat.common.core.enums.ImMessageType;
import com.tencreat.common.core.enums.ImSessionType;
import com.tencreat.common.core.utils.JwtService;
import com.tencreat.common.core.utils.ServletUtils;
import com.tencreat.common.entity.ImMessage;
import com.tencreat.common.entity.ImUser;
import com.tencreat.common.entity.ImUserSession;
import com.tencreat.common.entity.req.ReqIdAndPageParam;
import com.tencreat.common.entity.req.ReqLoginParam;
import com.tencreat.common.entity.req.ReqPageParam;
import com.tencreat.common.entity.req.ReqReadMsgParam;
import com.tencreat.common.entity.vo.UploadResult;
import com.tencreat.common.entity.vo.UserSessionVo;
import com.tencreat.common.service.ImMessageService;
import com.tencreat.common.service.ImSessionService;
import com.tencreat.common.service.ImUserSessionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共接口
 */
@RestController
@RequestMapping("/api/common")
public class ImCommonController extends BaseController{


    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public HttpResult avatar(MultipartFile file) throws IOException {
        OssClient storage = OssFactory.instance();
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        UploadResult uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        Map<String, Object> result = new HashMap<>();
        result.put("fileName", uploadResult.getFilename());
        result.put("url", uploadResult.getUrl());
        return HttpResult.ok(result);
    }




}
